package com.bjpowernode.service.impl;

import com.alibaba.excel.EasyExcel;
import com.bjpowernode.constant.Constant;
import com.bjpowernode.entity.TClue;
import com.bjpowernode.entity.TCustomer;
import com.bjpowernode.mapper.TClueMapper;
import com.bjpowernode.mapper.TCustomerMapper;
import com.bjpowernode.query.BaseQuery;
import com.bjpowernode.query.CustomerExcel;
import com.bjpowernode.query.CustomerQuery;
import com.bjpowernode.service.CustomerService;
import com.bjpowernode.util.LoginInfoUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@RequiredArgsConstructor
public class CustomerServiceImpl implements CustomerService {


    private final TCustomerMapper customerMapper;
    private final TClueMapper clueMapper;

    /**
     * 线索转客户
     * @param customerQuery
     * @return
     */
    @Transactional //事务
    @Override
    public boolean saveCustomer(CustomerQuery customerQuery) {
        //判断线索是否转换过客户，若已转过，则不能再转了
        TClue tClue = clueMapper.selectById(customerQuery.getClueId());
        if (tClue.getState() == -1) {
            throw new RuntimeException("该线索已转换过客户");
        }

        //插入客户表
        TCustomer tCustomer = new TCustomer();
        BeanUtils.copyProperties(customerQuery, tCustomer);
        tCustomer.setCreateBy(LoginInfoUtil.getCurrentLoginUser().getId());
        tCustomer.setCreateTime(new Date());

        int insert = customerMapper.insertSelective(tCustomer);


        //更新线索表 状态改成-1  已转客户
        TClue updateClue = new TClue();
        updateClue.setId(customerQuery.getClueId());
        updateClue.setState(-1);
        updateClue.setEditBy(LoginInfoUtil.getCurrentLoginUser().getId());
        updateClue.setEditTime(new Date());

        int update = clueMapper.updateByPrimaryKeySelective(updateClue);

        return insert > 0 && update > 0;
    }

    @Override
    public PageInfo<TCustomer> getCustomerByPage(Integer current) {
        PageHelper.startPage(current, Constant.PAGE_SIZE);
        List<TCustomer> tCustomerList = customerMapper.selectByPage(new BaseQuery(),null);
        PageInfo<TCustomer> pageInfo = new PageInfo<>(tCustomerList);
        return pageInfo;
    }

    /**
     * 导出excel
     * @param outputStream
     * @param idList
     */
    @Override
    public void exportExcel(OutputStream outputStream, List<String> idList) {
        List<TCustomer> tCustomerList = customerMapper.selectByPage(new BaseQuery(),idList);
        //将List<TCustomer> 转成 List<CustomerExcel>

        List<CustomerExcel> customerExcelList = new ArrayList<>();

        for (TCustomer tCustomer : tCustomerList) {
            CustomerExcel customerExcel = new CustomerExcel();

            customerExcel.setOwnerName(ObjectUtils.isEmpty(tCustomer.getOwnerDo()) ? "" : tCustomer.getOwnerDo().getName());
            customerExcel.setActivityName(ObjectUtils.isEmpty(tCustomer.getActivityDo()) ? "" : tCustomer.getActivityDo().getName());
            customerExcel.setFullName(ObjectUtils.isEmpty(tCustomer.getClueDo()) ? "" : tCustomer.getClueDo().getFullName());
            customerExcel.setAppellationName(ObjectUtils.isEmpty(tCustomer.getAppellationDo()) ? "" : tCustomer.getAppellationDo().getTypeValue());
            customerExcel.setPhone(ObjectUtils.isEmpty(tCustomer.getClueDo()) ? "" : tCustomer.getClueDo().getPhone());
            customerExcel.setWeixin(ObjectUtils.isEmpty(tCustomer.getClueDo()) ? "" : tCustomer.getClueDo().getWeixin());
            customerExcel.setNeedLoanName(ObjectUtils.isEmpty(tCustomer.getNeedLoanDo()) ? "" : tCustomer.getNeedLoanDo().getTypeValue());
            customerExcel.setIntentionStateName(ObjectUtils.isEmpty(tCustomer.getIntentionStateDo()) ? "" : tCustomer.getIntentionStateDo().getTypeValue());
            customerExcel.setSourceName(ObjectUtils.isEmpty(tCustomer.getSourceDo()) ? "" : tCustomer.getSourceDo().getTypeValue());
            customerExcel.setIntentionProductName(ObjectUtils.isEmpty(tCustomer.getIntentionProductDo()) ? "" : tCustomer.getIntentionProductDo().getName());
            customerExcel.setNextContactTime(tCustomer.getNextContactTime());
            customerExcelList.add(customerExcel);
            
        }

        //利用easyExcel解析集合数据
        EasyExcel.write(outputStream, CustomerExcel.class).sheet().doWrite(customerExcelList);


    }

    @Override
    public TCustomer getCustomerById(Integer id) {

        return customerMapper.selectById(id);
    }
}
